<template>
	<view class="app-container">
		<z-paging ref="paging" v-model="dataList" @query="queryList">
			<u-navbar slot="top" title="房源" :autoBack="false" placeholder bgColor="#cf141a"
				:titleStyle="{ color: '#fff' }">
				<view class="u-nav-slot" slot="left"></view>
			</u-navbar>
			<view class="search-item flex flex-between" slot="top">
				<u-search placeholder="房源标题或小区名称" v-model="keyword" animation :clearabled="true" bgColor="#fff"
					:actionStyle="{ color: '#a9a9a9' }" borderColor="#a9a9a9" height="70rpx" @custom="keywordCustom"
					@clear="keywordClear"></u-search>
				<view style="margin-left: 50rpx" class="flex flex-align-center" @click="pop.show = true"><u-icon
						name="list-dot" color="#999" size="28"></u-icon></view>
			</view>
			<view class="goods-list">
				<view class="list-item" v-for="(item, index) in dataList" :key="index"
					@click="$toPath.goPage('pages/listings/details', { id: item.id })">
					<view class="list-info flex">
						<u--image :showLoading="true" :src="item.img" width="246rpx" height="188rpx"></u--image>
						<view class="info">
							<u--text :text="item.title" :lines="2" block color="#000000" size="28rpx"></u--text>
							<view class="label flex">
								<u--text v-if="item.square !== '0'" :text="item.square" block align="left"
									color="#717171" size="28rpx"></u--text>
								<u--text v-if="item.structure !== '0'" :text="item.structure" block align="left"
									color="#717171" size="28rpx"></u--text>
							</view>
							<view class="price-item flex">
								<view class="item flex">
									<u--text text="起拍价" block align="left" color="#6e6e6e" size="24rpx"></u--text>
									<u--text :text="item.start_price" block align="left" color="#cf141a" size="28rpx"
										margin="0 10rpx 0"></u--text>
								</view>
								<view class="item flex">
									<u--text text="评估价" block align="left" color="#6e6e6e" size="24rpx"></u--text>
									<u--text :text="item.evaluate_price" block align="left" color="#cf141a" size="28rpx"
										margin="0 10rpx 0"></u--text>
								</view>
							</view>
						</view>
					</view>
					<view class="list-mode flex flex-between flex-align-center">
						<view class="count-item flex">
							<block v-if="item.status === '1' || item.status === '0'">
								<u--text :text="item.status === '0' ? '开拍时间：' : '结束时间：'" block align="left"
									color="#606060" size="26rpx"></u--text>
								<liu-countdown ref="countdown" :showSeconds="true" mode="2"
									:endDate="item.countdown_time" :auto="true"></liu-countdown>
							</block>
						</view>
						<view class="status flex flex-align-center flex-justify-center"
							:class="item.status === '1' ? 'error' : item.status === '0' ? 'succeed' : 'finish'">
							{{ item.status === '1' ? '正在拍卖' : item.status === '0' ? '即将开拍' : '拍卖结束' }}
						</view>
					</view>
				</view>
			</view>
		</z-paging>
		<u-popup :show="pop.show" mode="right" @close="pop.show = false" safeAreaInsetTop>
			<view class="pop-box">
				<u--form labelPosition="top" :model="pop.form" ref="uForm" labelWidth="200rpx">
					<u-form-item label="类型" borderBottom>
						<view class="tab flex">
							<view class="item" v-for="(item, index) in pop.typeList" :key="index">
								<u-tag :text="item.label" :borderColor="item.isTrue ? '#cf141a' : '#C0C0C0'"
									:bgColor="item.isTrue ? '#cf141a' : '#C0C0C0'" @click="selectTab(item)"></u-tag>
							</view>
						</view>
					</u-form-item>
					<u-form-item label="竞价状态" borderBottom>
						<view class="tab flex">
							<view class="item" v-for="(item, index) in pop.statusList" :key="index">
								<u-tag :text="item.name" :borderColor="item.isTrue ? '#cf141a' : '#C0C0C0'"
									:bgColor="item.isTrue ? '#cf141a' : '#C0C0C0'" @click="selectTab(item)"></u-tag>
							</view>
						</view>
					</u-form-item>
					<u-form-item label="竞价区域" borderBottom>
						<view class="tab flex">
							<view class="item" v-for="(item, index) in pop.districtList" :key="index">
								<u-tag :text="item.district" :borderColor="item.isTrue ? '#cf141a' : '#C0C0C0'"
									:bgColor="item.isTrue ? '#cf141a' : '#C0C0C0'" @click="selectTab(item)"></u-tag>
							</view>
						</view>
					</u-form-item>
					<u-form-item label="竞价时间" borderBottom @click="pop.time.visible = true">
						<u--input v-model="pop.time.label" disabled disabledColor="#ffffff" placeholder="请选择竞价时间"
							border="none" fontSize="28rpx"></u--input>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>
					<u-form-item label="竞价价格" borderBottom>
						<view class="flex flex-align-center">
							<!-- #ifndef APP-NVUE -->
							<u-input type="number" v-model="pop.form.start_price" placeholder="请输入起始价格"
								fontSize="28rpx">
							<!-- #endif -->
								<!-- #ifdef APP-NVUE -->
								<u--input type="number" v-model="pop.form.start_price" placeholder="请输入起始价格"
									fontSize="28rpx">
								<!-- #endif -->
									<template slot="suffix">
										<u--text text="万元" slot="prefix" margin="0" type="tips" size="28rpx"></u--text>
									</template>
							<!-- #ifndef APP-NVUE -->
							</u-input>
							<!-- #endif -->
							<!-- #ifdef APP-NVUE -->
							</u--input>
							<!-- #endif -->
							<view style="margin: 0 10rpx" class="flex flex-align-center">-</view>
							<!-- #ifndef APP-NVUE -->
							<u-input type="number" v-model="pop.form.end_price" placeholder="请输入结束价格" fontSize="28rpx">
							<!-- #endif -->
								<!-- #ifdef APP-NVUE -->
								<u--input type="number" v-model="pop.form.end_price" placeholder="请输入结束价格"
									fontSize="28rpx">
								<!-- #endif -->
									<template slot="suffix">
										<u--text text="万元" slot="prefix" margin="0" type="tips" size="28rpx"></u--text>
									</template>
							<!-- #ifndef APP-NVUE -->
							</u-input>
							<!-- #endif -->
							<!-- #ifdef APP-NVUE -->
							</u--input>
							<!-- #endif -->
						</view>
					</u-form-item>
					<u-form-item>
						<view class="flex">
							<u-button type="primary" text="重置" color="#cf141a" plain @click="reset"></u-button>
							<u-button type="primary" text="搜索" color="#cf141a" :customStyle="{ marginLeft: '20rpx' }"
								@click="search"></u-button>
						</view>
					</u-form-item>
				</u--form>
			</view>
		</u-popup>
		<w-picker :visible.sync="pop.time.visible" mode="range" startYear="1970" endYear="2999" themeColor="#cf141a"
			@confirm="onConfirm($event, 'range')" @cancel="onCancel" ref="range"></w-picker>
	</view>
</template>

<script>
	import wPicker from '@/components/w-picker/w-picker.vue';
	export default {
		components: {
			wPicker
		},
		data() {
			return {
				keyword: '',
				dataList: [],
				pop: {
					show: false,
					form: {
						type: '',
						status: '',
						starttime: '',
						endtime: '',
						start_price: '',
						end_price: '',
						district: ''
					},
					typeList: [{
							label: '住宅',
							value: '0',
							isTrue: false
						},
						{
							label: '商用',
							value: '1',
							isTrue: false
						},
						{
							label: '工业',
							value: '2',
							isTrue: false
						}
					],
					time: {
						visible: false,
						label: ''
					},
					statusList: [],
					districtList: []
				}
			};
		},
		onLoad() {
			this.apiHouseStatus()
			this.apiHouseAddress()
		},
		methods: {
			search() {
				let type = []
				for (var i = 0; i < this.pop.typeList.length; i++) {
					if (this.pop.typeList[i].isTrue) {
						type.push(this.pop.typeList[i].value)
					}
				}
				let status = []
				for (var i = 0; i < this.pop.statusList.length; i++) {
					if (this.pop.statusList[i].isTrue) {
						status.push(this.pop.statusList[i].status)
					}
				}
				let district = []
				for (var i = 0; i < this.pop.districtList.length; i++) {
					if (this.pop.districtList[i].isTrue) {
						district.push(this.pop.districtList[i].district)
					}
				}
				this.pop.form.type = type.join(',')
				this.pop.form.status = status.join(',')
				this.pop.form.district = district.join(',')
				this.$refs.paging.reload(false);
			},
			reset() {
				for (var i = 0; i < this.pop.typeList.length; i++) {
					this.pop.typeList[i].isTrue = false
				}
				for (var i = 0; i < this.pop.statusList.length; i++) {
					this.pop.statusList[i].isTrue = false
				}
				for (var i = 0; i < this.pop.districtList.length; i++) {
					this.pop.districtList[i].isTrue = false
				}
				this.pop.time.label = ''
				this.pop.form = {
					type: '',
					status: '',
					starttime: '',
					endtime: '',
					start_price: '',
					end_price: '',
					district: ''
				}
				this.keyword = ''
				this.$refs.paging.reload(false);
			},
			onConfirm(event, key) {
				console.log(event);
				console.log(key);
				if (key === 'range') {
					this.pop.time.label = event.result;
					this.pop.form.starttime = event.value[0];
					this.pop.form.endtime = event.value[1];
				}
			},
			onCancel(e) {
				console.log(e);
			},
			apiHouseAddress() {
				this.$api.apiHouseAddress().then((res) => {
					console.log(res.list);
					for (var i = 0; i < res.list.length; i++) {
						res.list[i].isTrue = false;
					}
					this.pop.districtList = res.list;
				});
			},
			apiHouseStatus() {
				this.$api.apiHouseStatus().then((res) => {
					console.log(res.list);
					for (var i = 0; i < res.list.length; i++) {
						res.list[i].isTrue = false;
					}
					this.pop.statusList = res.list;
				});
			},
			selectTab(item) {
				item.isTrue = !item.isTrue;
				this.$forceUpdate();
			},
			keywordClear(event) {
				this.$refs.paging.reload(false);
			},
			keywordCustom(event) {
				this.$refs.paging.reload(false);
			},
			queryList(pageNo) {
				this.$api
					.apiHouseList({
						page: pageNo,
						keyword: this.keyword,
						...this.pop.form
					})
					.then((res) => {
						this.pop.show = false
						this.$refs.paging.complete(res.house_list);
					});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.app-container {
		.search-item {
			padding: 20rpx 30rpx;
		}

		.pop-box {
			padding: 60rpx 30rpx 0;
			width: 480rpx;

			.tab {
				flex-wrap: wrap;

				.item {
					margin-right: 20rpx;
					margin-bottom: 20rpx;

					/deep/ .u-tag {
						justify-content: center;
					}
				}
			}
		}

		.tabs {
			margin-top: 35rpx;

			/deep/ .u-text {
				flex: none;
				width: auto;
			}
		}
	}
</style>